package com.app.twth.ui.adjust.redamage.destroy;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityRedamageDestroyBinding;
import com.app.twth.databinding.DialogRedamageDetailBinding;
import com.app.twth.ui.adjust.redamage.ReDamage;
import com.app.twth.ui.adjust.redamage.ReDamageAdapter;
import com.app.twth.ui.adjust.redamage.ReDamageQr;
import com.app.twth.ui.adjust.redamage.oldsku.RDOldSkuViewModel;
import com.app.twth.utils.MyDialogUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

public class RDDestroyActivity extends BaseActivity<ActivityRedamageDestroyBinding, RDOldSkuViewModel> {
    private String TAG = getClass().getSimpleName();
    private ReDamage reDamage;
    private ReDamageQr reDamageQr;

    @Override
    protected String setTitle() {
        return "Damage(Destroy)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext(getString(R.string.submit));

        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        reDamage = (ReDamage) bundle.getSerializable("data");

        binding.tvContaier.setText(reDamage.getContainerNo());
        binding.tvIblpn.setText(reDamage.getLpn());
        binding.tvSkuQuantity.setText(reDamage.getSku() + "(" + reDamage.getQty() + ")");
        binding.tvCodeLpn.requestFocus();
    }

    @Override
    protected void initListener() {
        viewModel.lpnPhotoSubmitMutableLiveData.observe(this, data -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        showHint("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_sku:
                if (reDamage.getProcessList() == null) {
                    return;
                }
                if (reDamage.getProcessList().size() == 0) {
                    return;
                }
                showReDamageDetailDialog();
                break;
            case R.id.iv_scan_lpn:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCodeLpn.setText("");
                showHint("");
                break;
            case R.id.tv_next:
                submitData();
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCodeLpn.setText(barcodeStr);
    }

    private void submitData() {
        if (System.currentTimeMillis() - timeMillis < 2000) {
            return;
        }
        timeMillis = System.currentTimeMillis();

        String lpn = binding.tvCodeLpn.getText().toString();

        if (Utils.isEmpty(lpn)) {
            ToastUtil.showToast("Please enter the lpn");
            return;
        }
        if (reDamageQr == null) {
            reDamageQr = new ReDamageQr();
        }

        reDamageQr.setTaskId(reDamage.getTaskId());
        reDamageQr.setObLpn(lpn);
        viewModel.damageProcessSubmit(reDamageQr);
    }

    private AlertDialog dialogReDamageDetail;

    private void showReDamageDetailDialog() {
        dialogReDamageDetail = MyDialogUtil.getDialog(mContext, initReDamageDetailDialogView(), Gravity.CENTER);
        dialogReDamageDetail.setCanceledOnTouchOutside(true);
        dialogReDamageDetail.show();
    }

    ReDamageAdapter tallyDetailAdapter;

    private View initReDamageDetailDialogView() {
        DialogRedamageDetailBinding dialogBinding = DialogRedamageDetailBinding.inflate(getLayoutInflater());

        tallyDetailAdapter = new ReDamageAdapter(this, reDamage.getProcessList());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        dialogBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
        dialogBinding.recyclerviewDetail.setAdapter(tallyDetailAdapter);

        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialogReDamageDetail != null) {
                dialogReDamageDetail.dismiss();
            }
        });
        return dialogBinding.getRoot();
    }
}
